﻿using System;
using System.Linq;
using System.Reactive.Linq;
using System.Windows;
using Reactive.Bindings;
using System.Reactive;
using Reactive.Bindings.Extensions;

namespace WpfApp1.Views
{
    public partial class TestReactivePropertyView : Window
    {
        public TestReactivePropertyView()
        {
            InitializeComponent();
        }

        protected override void OnClosed(EventArgs e)
        {
            if (DataContext is TestReactivePropertyViewModel vm)
            {
                vm.Close();
            }
            base.OnClosed(e);
        }
    }

    public class TestReactivePropertyViewModel : ReactiveBindableBase
    {
        public TestReactivePropertyViewModel()
        {
            Input = new ReactivePropertySlim<string>("");
            TieObservable(() => Input);
            TieObservable(() => 
                Observable.Interval(TimeSpan.FromSeconds(1))
                    .Select(tt => DisplayTime = tt.ToString())
            );

            Output = Input
                .Delay(TimeSpan.FromSeconds(1))
                .Select(x => x.ToUpper())
                .ObserveOnUIDispatcher()
                .ToReadOnlyReactivePropertySlim();
        }

        public ReactivePropertySlim<string> Input { get; }
        public ReadOnlyReactivePropertySlim<string?> Output { get; }

        private string displayTime = "";
        public string DisplayTime { get => displayTime; set => SetProperty(ref displayTime, value); }

        public void Close()
        {
            Input.Dispose();
            //output.Dispose();       
        }
    }
}
